<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\AddressRequest;
use Auth;
use App\Models\Address;

class AddressController extends Controller
{
    public function index()
    {
        $datas = Auth::user()->addresses()->orderBy('is_default', 'desc')->get();
    	return view('home.address.index', ['datas' => $datas]);
    }

    public function create()
    {
    	return view('home.address.create');
    }

    public function store(AddressRequest $request)
    {
        $user = Auth::user();
        $user->addresses()->create($request->all());
        return redirect()->route('address.index');
    }

    public function edit($id)
    {
        $address = Auth::user()->addresses()->findOrFail($id);
        return view('home.address.create', ['data' => $address]);
    }

    public function update(AddressRequest $request, $id) {
    	$address = Auth::user()->addresses()->findOrFail($id);
    	$address->update($request->all());
    	return redirect()->route('address.index');
    }

    public function destroy($id) {
    	$address = Auth::user()->addresses()->findOrFail($id);
    	$address->delete();
    	return response()->json(['status' => 0]);
    }
}
